<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/9/30
 * Time: 16:27
 */

namespace app\common\model;


use think\Exception;

class Banner extends BaseModel
{

    public function getImageAttr($value) {
        return str_replace('\\', '/', $value);
    }

    /**
     * 按排序获取所有banner
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList() {
        return self::order('sort asc')->select();
    }

    /**
     * 创建或者编辑banner
     * @param $param
     * @param int $id
     * @return \think\response\Json
     */
    public static function createOrUpdateBanner($param, $id = 0) {
        try {
            if ($id > 0) {
                // 编辑
                $banner = self::get($id);
                if (!$banner) {
                    throw new Exception('banner不存在');
                }
                $banner->save($param);
            } else {
                // 添加
                self::create($param);
            }
            return return_data('success');
        } catch (Exception $e) {
            return return_data($e->getMessage(), 1);
        }

    }
}